<?php
/**
 * descript: 自定义异常处理器
 * User: blue
 * Date: 2018/5/7 10:58
 */
namespace app\libs\exception;


use think\exception\Handle;
use think\facade\Log;

class ExceptionHandler extends Handle {
    private $httpCode;
    private $message;
    private $errCode;
    //需要返回客户端当前请求的url路径
    /**
     * @param Exception $e
     * @return \think\Response|void
     */
    public function render(\Exception $e)
    {
        if($e instanceof BaseException){
            $this->httpCode=$e->httpCode;
            $this->message=$e->message;
            $this->errCode=$e->errCode;
        }else{
            if(config('app.app_debug')){
                return parent::render($e);
            }else{
                $this->httpCode=500;
                $this->message="server error";
                $this->errCode=ErrDict::SERVER_ERROR;
                //日志记录实际的异常信息
                Log::write($e->getMessage(),'error');
            }
        }
        $url=request()->url();
        $status=[
            'message'=>$this->message,
            'errCode'=>$this->errCode,
            'url'=>$url,
        ];
        return json($status,$this->httpCode);
    }
}